// pages/tools/ertong/csjbxx.js
var _$ = require("../../../common/wxjs/wx_ajax");
const url = "et_bbjbxx";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        uesrtoken: null,
        formdata: {
            "cs_data": '请选择日期',
            "cs_time": '请选择时间',
            "cs_address": '',
            "cs_yxzm": '',
            "cs_hospital": '',
            "cs_sex": '0',
            "cs_week": '',
            "cs_tz": '',
            "cs_sc": ''
        }
    },

    btnselect: function(e) {
        var that = this;
        that.setData({
            // 母亲数据
            "formdata.cs_sex": e.target.dataset.cs_sex,

        })
    },
    // 提交表单
    formSubmit: function(e) {
        var that = this;
        var userdata = e.detail.value;
        userdata = JSON.stringify(userdata);
        var litToken = that.data.uesrtoken;
        var type = 1;
        var methods = "POST";
        var header = "application/x-www-form-urlencoded";
        var dataobj = { type, userdata, litToken };
        wx.showToast({
            title: '正在提交',
            icon: 'loading',
            duration: 2000
        })
        _$.sendRrquest(url, methods, dataobj, header)
            .then(function(response) {
                console.log(response)
                if (response.data.status == 200) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                } else {
                    wx.showToast({
                        title: '数据未修改',
                        icon: 'loading',
                        duration: 2000
                    });
                    setTimeout(function() {
                        wx.hideToast()
                    }, 2000)
                }
            }, function(error) {
                wx.showToast({
                    title: '提交失败',
                    duration: 2000
                });
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    // 请求数据
    getdata: function(val) {
        var that = this;
        var type = 0;
        var litToken = val;
        var dataobj = { type, litToken };
        var methods = "GET";
        var header = "application/json";
        wx.showLoading({
            title: '加载中',
        })
        _$.sendRrquest(url, methods, dataobj, header)
            .then(function(response) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 1000)
                console.log(response)
                that.setData({
                    formdata: response.data.data.res
                });
                if (response.data.data.res.cs_data == "") {
                  that.setData({
                    "formdata.fm_data": "请点击选择日期"
                  });
                }
                if (response.data.data.res.cs_time == "") {
                  that.setData({
                    "formdata.fm_data": "请点击选择时间"
                  });
                }
            }, function(error) {
                setTimeout(function() {
                    wx.hideLoading()
                }, 2000)
                wx.showToast({
                    title: '网络错误，请稍后再试',
                    duration: 2000
                })
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
                console.log(error);
            });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        wx.getStorage({
            key: 'token',
            success: function(res) {
                that.setData({
                    uesrtoken: res.data
                })
                that.getdata(res.data)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    csData: function(e) {
        this.setData({
            "formdata.cs_data": e.detail.value
        })
    },
    csTime: function(e) {
        this.setData({
            "formdata.cs_time": e.detail.value
        })
    }
})